<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="customerClassName"
            placeholder="请输入分类名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="customerClassOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            default-expand-all
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>

      <el-col :span="20" :xs="24">
        <div v-if="!isDialog">
          <el-breadcrumb separator="/">
            <el-breadcrumb-item>首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统管理</el-breadcrumb-item>
            <el-breadcrumb-item>客户管理</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="客户编码" prop="ccccode">
            <el-input
              v-model="queryParams.ccuscode"
              placeholder="请输入客户编码"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="客户名称" prop="ccusname">
            <el-input
              v-model="queryParams.ccusname"
              placeholder="请输入客户名称"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <el-table v-loading="loading" :data="CustomerList" @row-dblclick="handleRowDblClick">
          <el-table-column label="客户编码" align="left" width="200" prop="ccuscode"/>
          <el-table-column label="客户名称" align="left" prop="ccusname"/>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button type="text" @click="handleShowContact(scope.row)">联系人</el-button>
              <el-button type="text" @click="handleShowAddress(scope.row)">收货地址</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <el-dialog
      title="联系人"
      :visible.sync="showContactDialog"
      width="90%"
      @close="handleCloseContact"
      append-to-body>
      <customer-contact
        v-if="showContactDialog && selectedCustomer"
        :ccuscode="selectedCustomer.ccuscode"
        :key="'contact-' + (selectedCustomer ? selectedCustomer.ccuscode : '')"
      />
    </el-dialog>
    <el-dialog
      title="收货地址"
      :visible.sync="showAddressDialog"
      width="90%"
      @close="handleCloseAddress"
      append-to-body  :z-index="2700">
      <customer-address
        v-if="showAddressDialog && selectedCustomer"
        :ccuscode="selectedCustomer.ccuscode"
        :key="'address-' + (selectedCustomer ? selectedCustomer.ccuscode : '')"
      />
    </el-dialog>
  </div>
</template>

<script>
import {  listCustomer,  treeselect } from "@/api/business/Customer";
import    CustomerAddress             from  '@/views/business/address/index.vue'
import    CustomerContact              from  '@/views/business/contact/index.vue'


export default {
  name: "Customer",
  props: {
    isDialog: {
      type: Boolean,
      default: false
    }
  },
  components: {CustomerAddress,CustomerContact},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      customerClassName: undefined,
      customerClassOptions: undefined,
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // Customer表格数据
      CustomerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 字典
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ccusname: null,
        ccuscode: null,
        ccccode: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      showContactDialog: false,
      showAddressDialog: false,
      selectedCustomer: null,
    };
  },
  watch: {
    // 根据名称筛选部门树
    customerClassName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getList();
    this.getTreeselect();
  },
  methods: {
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.customerClassOptions = response.data;
      });
    },


    handleNodeClick(data) {
      this.queryParams.ccccode = data.id;
      this.getList();
    },

    /** 查询Customer列表 */
    getList() {
      this.loading = true;
      listCustomer(this.queryParams).then(response => {
        this.CustomerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ccuscode: null,
        ccusname: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleRowDblClick(row) {
      if (this.isDialog) {
        this.$emit('row-dblclick', row);
      }
    },
    handleShowContact(row) {
      this.selectedCustomer = row;
      this.showContactDialog = false;
      this.$nextTick(() => {
      this.showContactDialog = true;
      });
    },
    handleShowAddress(row) {
      this.selectedCustomer = row;
      this.showAddressDialog = false;
      this.$nextTick(() => {
      this.showAddressDialog = true;
      });
    },
    handleCloseContact() {
      this.showContactDialog = false;
    },
    handleCloseAddress() {
      this.showAddressDialog = false;
    },
  }
};
</script>
